<?php

namespace App\Admin\Controllers;

use App\User;

use Carbon\Carbon;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class UserController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('会员列表');
            $content->description('会员列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑会员');
            $content->description('编辑会员');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('添加会员');
            $content->description('添加会员');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $res = Admin::grid(User::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->column('name', '用户名');
            $grid->column('email', '邮箱')->display(function ($email){
                $is_active = User::where('email', $email)->first()->is_active;
                $str = '<font color="gray">(未验证)</font>';
                if ($is_active == 1) $str = '<font color="green">(已验证)</font>';
                return $email. ' ' .$str;
            });

            $grid->column('avatar', '图像')->display(function ($avatar){
                return '<div class="image"><img src="'.$avatar.'"  style="width: 40px; height: 40px;"/></div>';
            });
            $grid->column('sex', '性别')->display(function ($sex) {
                if ($sex == 2){
                    return '<label class="label label-success">男</label>';
                }elseif($sex == 1){
                    return '<label class="label bg-red">女</label>';
                }elseif ($sex == 3){
                    return '<label class="label label-default">保密</label>';
                }
            });
            $grid->column('posts_count', '主题数量');
            $grid->column('comments_count', '回复数量');
            $grid->column('register_time', '注册时间');
            $grid->column('register_ip', '注册ip')->display(function ($ip){
                return '<label class="label label-info">'.$ip.'</label>';
            });

            $states = [
                'on'  => ['value' => 1, 'text' => '正常', 'color' => 'primary'],
                'off' => ['value' => 0, 'text' => '锁定', 'color' => 'default'],
            ];
            $grid->status('状态')->switch($states);

//            $grid->released('状态')->display(function ($released) {
//                if ($released == 0){
//                    return '<label class="label label-success">正常</label>';
//                }elseif($released == 1){
//                    return '<label class="label label-warning">锁定</label>';
//                }
//            });
            $grid->column('login_last_time', '最后登录时间');
        });

        return $res;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(User::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('name', '用户名')->rules(function ($form){
                return 'required|unique:users,name,'.$form->model()->id.'';
            });
            $form->email('email', '邮箱')->rules(function ($form){
                return 'required|unique:users,email,'.$form->model()->id.'';
            });
//            $form->image('avatar', '图像')->move(attachment().'avatar/'.date('Ymd'))->uniqueName();
            $directors = [
                1   => '女',
                2   => '男',
                3   => '保密',
            ];
            $form->select('sex', '性别')->options($directors)->rules('required');
            $form->password('password', '密码')->rules('required')->default(function ($form){
                return $form->model()->password;
            });
            $form->saving(function (Form $form) {
                if ($form->password && $form->model()->password != $form->password) {
                    $form->password = bcrypt($form->password);
                }
            });

            $form->select('is_active', '验证邮箱')->options(['未验证', '已验证'])->rules('required');

            $states = [
                'on'  => ['value' => 1, 'text' => '正常', 'color' => 'primary'],
                'off' => ['value' => 0, 'text' => '锁定', 'color' => 'default'],
            ];

            $form->switch('status', '状态')->options($states)->default(1);

            $form->hidden('confirm_token', 'confirm_token')->default(str_random(40));
            $form->hidden('confirm_token', 'confirm_token')->default(str_random(40));
            $form->hidden('register_time', 'register_time')->default(function ($form){
                $register_time = $form->model()->register_time;
                if ($register_time){
                    return $register_time;
                }else{
                    return Carbon::now();
                }
            });
            $form->hidden('register_ip', 'register_ip')->default('admin');
            $form->display('updated_at', 'Updated At');

        });
    }
}
